package 二叉树系列;

import java.util.LinkedList;
import java.util.List;

public class BinaryTreePaths {
    List<TreeNode> list = new LinkedList<>();
    List<String> ans = new LinkedList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        findTree(root);
        return ans;
    }
    public void findTree(TreeNode node){
        if(node==null)  return ;

        list.add(node);
        findTree(node.left);
        if(node.left!=null)  list.remove(node.left);
        findTree(node.right);
        if(node.right!=null)  list.remove(node.right);
        if(node.left==null&&node.right==null){
            StringBuilder sb = new StringBuilder();
            for(TreeNode treeNode:list){
                sb.append(treeNode.val+"->");
            }
            sb.delete(sb.length()-2,sb.length());
            ans.add(String.valueOf(sb));
        }
    }
}
